package com.admission.service;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.admission.dao.ArrayInfoDao;
import com.admission.dao.EnvironmentDao;
import com.admission.model.ArrayInfo;
import com.admission.model.Environment;

@Repository
@Transactional
public class EnvironmentManager {
	private EnvironmentDao environmentDao;
	private EnvironmentPictureManager environmentPictureManager;
	private ArrayInfoDao arrayInfoDao;
	private SessionFactory sessionFactory;

	/**
	 * 通过id查找位置对象，无返回null
	 * 
	 * @param id
	 * @return
	 */
	public Environment getById(String id) {
		return environmentDao.findUniqueBy("id", id);
	}

	/**
	 * 批量删除 逗号隔开id集合
	 * 
	 * @param ids
	 */
	public void deleteByIds(String ids) {
		for (String id : ids.split(",")) {
			environmentPictureManager.deleteByIds(id.trim());
			environmentDao.delete(id.trim());
		}
	}

	/**
	 * 检查类型重复
	 * 
	 * @param newType
	 * @param oldType
	 * @return
	 */
	public List<Environment> checkType() {
		// return environmentDao.findUniqueBy("type", type);
		String hql = "from ArrayInfo a where itemValue not in (select e.type from Environment e)";
		return environmentDao.find(hql);

	}

	/**
	 * 通过字典大类查找小类list
	 * 
	 * @param value
	 * @return
	 */
	public List<Environment> getPageByValue(String value) {
		String sql = "SELECT environment.id,environment.content ,type FROM arrayinfo,environment WHERE item_name = environment.type AND p_array_info=(SELECT id FROM arrayinfo WHERE item_name ='"
				+ value + "')";
		SQLQuery query = environmentDao.getSessionFactory().getCurrentSession()
				.createSQLQuery(sql).addEntity(Environment.class);
		List<Environment> list = (List<Environment>) query.list();
		return list;
	}

	public List<ArrayInfo> getByBigType(String name) {
		ArrayInfo a = arrayInfoDao.findUniqueBy("itemName", name);
		List<ArrayInfo> l = arrayInfoDao.findBy("arrayInfo.id", a.getId());
		return l;
	}

	public boolean getByValue(String value) {
		Environment e = environmentDao.findUniqueBy("type", value);
		if (e != null)
			return true;
		else
			return false;
	}

	/**
	 * 根据数据字典小类获取对象
	 * 
	 * @param type
	 * @return
	 */
	public Environment getByType(String type) {
		return environmentDao.findUniqueBy("type", type);
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public void save(Environment entity) {
		environmentDao.save(entity);
	}

	/**
	 * 通过filter分页查询
	 * 
	 * @param page
	 * @param filters
	 * @return
	 */
	public Page<Environment> pageByFilter(Page<Environment> page,
			List<PropertyFilter> filters) {
		return environmentDao.findPage(page, filters);
	}

	/**
	 * 改变状态
	 * 
	 * @param id
	 * @param status
	 */
	public void changeConsult(String status) {
		ArrayInfo a = arrayInfoDao.findUniqueBy("itemName",
				ArrayInfo.ENVIRONMENT);
		a.setItemValue(status);
		arrayInfoDao.save(a);
	}

	/**
	 * 判断权限
	 * 
	 * @return
	 */
	public String checkConsult() {
		ArrayInfo a = arrayInfoDao.findUniqueBy("itemName",
				ArrayInfo.ENVIRONMENT);
		return a.getItemValue();
	}

	public EnvironmentDao getEnvironmentDao() {
		return environmentDao;
	}

	@Autowired
	public void setEnvironmentDao(EnvironmentDao environmentDao) {
		this.environmentDao = environmentDao;
	}

	public EnvironmentPictureManager getEnvironmentPictureManager() {
		return environmentPictureManager;
	}

	@Autowired
	public void setEnvironmentPictureManager(
			EnvironmentPictureManager environmentPictureManager) {
		this.environmentPictureManager = environmentPictureManager;
	}

	public ArrayInfoDao getArrayInfoDao() {
		return arrayInfoDao;
	}

	@Autowired
	public void setArrayInfoDao(ArrayInfoDao arrayInfoDao) {
		this.arrayInfoDao = arrayInfoDao;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
